package com.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.entity.Reservation;
import com.museum.entity.Review;
import com.museum.entity.User;
import com.museum.mapper.ReviewMapper;
import com.museum.service.ReservationService;
import com.museum.service.ReviewService;
import com.museum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价服务实现类
 */
@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements ReviewService {

    @Autowired
    private UserService userService;

    @Autowired
    private ReservationService reservationService;

    @Override
    public boolean addReview(Review review) {
        if (review.getReservationId() == null || review.getUserId() == null || review.getContent() == null) {
            return false;
        }
        
        // 检查是否已评价
        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Review::getReservationId, review.getReservationId());
        Long count = this.count(queryWrapper);
        if (count > 0) {
            // 已经评价过
            return false;
        }
        
        // 设置创建时间
        review.setCreateTime(LocalDateTime.now());
        return this.save(review);
    }

    @Override
    public boolean replyReview(Long reviewId, Long adminId, String reply) {
        if (reviewId == null || adminId == null || reply == null) {
            return false;
        }
        
        Review review = this.getById(reviewId);
        if (review == null) {
            return false;
        }
        
        review.setAdminId(adminId);
        review.setReply(reply);
        review.setReplyTime(LocalDateTime.now());
        
        return this.updateById(review);
    }

    @Override
    public List<Map<String, Object>> getUserReviews(Long userId) {
        if (userId == null) {
            return new ArrayList<>();
        }
        
        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Review::getUserId, userId);
        queryWrapper.orderByDesc(Review::getCreateTime);
        
        List<Review> reviews = this.list(queryWrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        
        for (Review review : reviews) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", review.getId());
            map.put("reservationId", review.getReservationId());
            map.put("content", review.getContent());
            map.put("reply", review.getReply());
            map.put("score", review.getScore());
            map.put("createTime", review.getCreateTime());
            map.put("replyTime", review.getReplyTime());
            
            // 获取预约信息
            Reservation reservation = reservationService.getById(review.getReservationId());
            if (reservation != null) {
                map.put("visitDate", reservation.getVisitDate());
                map.put("visitTimeSlot", reservation.getVisitTimeSlot());
            }
            
            result.add(map);
        }
        
        return result;
    }

    @Override
    public Map<String, Object> getReservationReview(Long reservationId) {
        if (reservationId == null) {
            return null;
        }
        
        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Review::getReservationId, reservationId);
        
        Review review = this.getOne(queryWrapper);
        if (review == null) {
            return null;
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("id", review.getId());
        result.put("reservationId", review.getReservationId());
        result.put("userId", review.getUserId());
        result.put("content", review.getContent());
        result.put("reply", review.getReply());
        result.put("score", review.getScore());
        result.put("createTime", review.getCreateTime());
        result.put("replyTime", review.getReplyTime());
        
        // 获取用户信息
        User user = userService.getById(review.getUserId());
        if (user != null) {
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", user.getId());
            userInfo.put("nickname", user.getNickname());
            userInfo.put("avatar", user.getAvatar());
            result.put("userInfo", userInfo);
        }
        
        return result;
    }

    @Override
    public Page<Map<String, Object>> pageReviews(Page<Review> page, Boolean hasReply) {
        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        
        // 是否已回复筛选
        if (hasReply != null) {
            if (hasReply) {
                queryWrapper.isNotNull(Review::getReply);
            } else {
                queryWrapper.isNull(Review::getReply);
            }
        }
        
        queryWrapper.orderByDesc(Review::getCreateTime);
        
        Page<Review> reviewPage = this.page(page, queryWrapper);
        Page<Map<String, Object>> resultPage = new Page<>(
                reviewPage.getCurrent(),
                reviewPage.getSize(),
                reviewPage.getTotal());
        
        List<Map<String, Object>> records = new ArrayList<>();
        
        for (Review review : reviewPage.getRecords()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", review.getId());
            map.put("reservationId", review.getReservationId());
            map.put("userId", review.getUserId());
            map.put("content", review.getContent());
            map.put("reply", review.getReply());
            map.put("adminId", review.getAdminId());
            map.put("score", review.getScore());
            map.put("createTime", review.getCreateTime());
            map.put("replyTime", review.getReplyTime());
            
            // 获取用户信息
            User user = userService.getById(review.getUserId());
            if (user != null) {
                Map<String, Object> userInfo = new HashMap<>();
                userInfo.put("id", user.getId());
                userInfo.put("nickname", user.getNickname());
                userInfo.put("avatar", user.getAvatar());
                map.put("userInfo", userInfo);
            }
            
            // 获取预约信息
            Reservation reservation = reservationService.getById(review.getReservationId());
            if (reservation != null) {
                Map<String, Object> reservationInfo = new HashMap<>();
                reservationInfo.put("id", reservation.getId());
                reservationInfo.put("visitDate", reservation.getVisitDate());
                reservationInfo.put("visitTimeSlot", reservation.getVisitTimeSlot());
                reservationInfo.put("visitorCount", reservation.getVisitorCount());
                map.put("reservationInfo", reservationInfo);
            }
            
            records.add(map);
        }
        
        resultPage.setRecords(records);
        return resultPage;
    }
} 